// 设置文件
import setting from '@/setting.js';

export default {
  namespaced: true,
  state: {
    // 顶栏菜单
    header: [],
    // 侧栏菜单
    aside: [],
    // 侧边栏收缩
    asideCollapse: setting.menu.asideCollapse,
    // 侧栏菜单标题
    asideTitle: '',
    allMenu: {},
  },
  actions: {
    /**
     * 设置侧边栏展开或者收缩
     * @param {Object} state vuex state
     * @param {Boolean} collapse is collapse
     */
    asideCollapseSet({ state, dispatch }, collapse) {
      // eslint-disable-next-line no-async-promise-executor
      return new Promise(async (resolve) => {
        // store 赋值
        state.asideCollapse = collapse;
        // 持久化
        await dispatch(
          'vxadmin/db/set',
          {
            dbName: 'sys',
            path: 'menu.asideCollapse',
            value: state.asideCollapse,
            user: true,
          },
          { root: true },
        );
        // end
        resolve();
      });
    },
    /**
     * 切换侧边栏展开和收缩
     * @param {Object} state vuex state
     */
    asideCollapseToggle({ state, dispatch }) {
      // eslint-disable-next-line no-async-promise-executor
      return new Promise(async (resolve) => {
        // store 赋值
        state.asideCollapse = !state.asideCollapse;
        // 持久化
        await dispatch(
          'vxadmin/db/set',
          {
            dbName: 'sys',
            path: 'menu.asideCollapse',
            value: state.asideCollapse,
            user: true,
          },
          { root: true },
        );
        // end
        resolve();
      });
    },
    /**
     * 从持久化数据读取侧边栏展开或者收缩
     * @param {Object} state vuex state
     */
    asideCollapseLoad({ state, dispatch }) {
      // eslint-disable-next-line no-async-promise-executor
      return new Promise(async (resolve) => {
        // store 赋值
        state.asideCollapse = await dispatch(
          'vxadmin/db/get',
          {
            dbName: 'sys',
            path: 'menu.asideCollapse',
            defaultValue: setting.menu.asideCollapse,
            user: true,
          },
          { root: true },
        );
        // end
        resolve();
      });
    },
    /**
     * 初始化菜单
     * @param {Array} menu tree
     * add by lss
     */
    // eslint-disable-next-line no-unused-vars
    initMenu({ state, dispatch, commit }, menu) {
      commit('headerSet', menu);
      dispatch(
        'vxadmin/db/set',
        {
          dbName: 'sys',
          path: 'menu.menuTree',
          value: menu,
          user: true,
        },
        { root: true },
      );
    },
    /**
     * 从持久化数据中恢复菜单
     * add by lss
     */
    loadMenu({ dispatch }) {
      return dispatch(
        'vxadmin/db/get',
        {
          dbName: 'sys',
          path: 'menu.menuTree',
          user: true,
        },
        { root: true },
      );
    },
  },
  mutations: {
    /**
     * @description 设置顶栏菜单
     * @param {Object} state vuex state
     * @param {Array} menu menu setting
     */
    headerSet(state, menu) {
      // store 赋值
      state.header = menu;
    },
    /**
     * @description 设置侧边栏菜单
     * @param {Object} state vuex state
     * @param {Array} menu menu setting
     */
    asideSet(state, menu) {
      // store 赋值
      state.aside = menu;
    },
    asideTitleSet(state, title) {
      state.asideTitle = title;
    },
  },
};
